package com.chinatsp.onlineapp.base;

import lib.http.R;
import tsp.cn.http.interfac.INetResult;
import tsp.cn.http.tools.T;
import tsp.cn.http.widget.ProgressHUD;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;

/**
 * Created by nus on 16-10-20.
 */

public class BaseFragment extends Fragment implements INetResult {


    ProgressHUD mProgressHUD;
    private boolean isPrepared;
    
    
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    	super.onActivityCreated(savedInstanceState);
    	  initPrepare();
    }
    
    
    private boolean isFirstResume = true;
    
    @Override
    public void onResume() {
    	super.onResume();
    	if (isFirstResume) {
    		   isFirstResume = false;
    		   return;
		}
    	if (getUserVisibleHint()) {
    	    onUserVisible();
		}
    	
    }
    
    @Override
    public void onPause() {
    	super.onPause();
    	if (getUserVisibleHint()) {
    		 onUserInvisible();
		}
    	
    }
    private boolean isFirstVisible = true;
    private boolean isFirstInvisible = true;

    @Override
     public void setUserVisibleHint(boolean isVisibleToUser) {
          super.setUserVisibleHint(isVisibleToUser);
          if (isVisibleToUser) {
              if (isFirstVisible) {
                  isFirstVisible = false;
                  initPrepare();
              } else {
                  onUserVisible();
              }
          } else {
              if (isFirstInvisible) {
                  isFirstInvisible = false;
                  onFirstUserInvisible();
              } else {
                  onUserInvisible();
              }
          }
      }
  
      public synchronized void initPrepare() {
          if (isPrepared) {
              onFirstUserVisible();
          } else {
              isPrepared = true;
          }
      }
  
      /**
       * 第一次fragment可见（进行初始化工作）
       */
      public void onFirstUserVisible() {
  
      }
  
      /**
       * fragment可见（切换回来或者onResume）
       */
      public void onUserVisible() {
    	  
  
      }
  
      /**
       * 第一次fragment不可见（不建议在此处理事件）
       */
      public void onFirstUserInvisible() {
  
      }
  
      /**
       * fragment不可见（切换掉或者onPause）
       */
      public void onUserInvisible() {
  
      }
 
 
    
    
    /**
     *
     * @param requestCode 网络请求顺序号，第一个请求，NetRequestOrderNum=0,处理第一条请求的结果。如果等于1,
     */
    @Override
    public void onRequestSuccess(int requestCode) {

    }

    /**
     * 请求失败
     */
    @Override
    public void onFinished() {
        showProgress(false);
    }

    /**
     * 请求失败
     * @param errorNo
     * @param errorMessage
     */
    @Override
    public void onRequestFailed(String errorNo, String errorMessage) {
        T.showShort(getActivity(), errorMessage);
    }


    /**
     * 无网络连接
     */
    @Override
    public void onNoConnect() {
//        showProgress(false);
        T.showShort(getActivity(), "无网络连接");
    }

    

    /**
     * 加载对话框
     * @param show
     */
    public void showProgress(boolean show) {

    	if (getActivity() != null) {
    		showProgressWithText(show, getActivity().getString(R.string.loading));	
		}        

    }


    /**
     * 可编辑 提示信息的加载对话框
     * @param show
     * @param message
     */
    public void showProgressWithText(boolean show, String message) {
        if (show) {
            mProgressHUD = ProgressHUD.show(getActivity(), message, true, true, null);
        } else {
            if (mProgressHUD != null) {
                mProgressHUD.dismiss();
            }
        }
    }
}
